package main

import (
  "fmt"
  "strings"
)

func testStr()  {
  var a string
  fmt.Println(a) //默认是空串


  //=============================
  var b = "hello"
  fmt.Println(b) //hello


  //============================
  // c := b ---> 相当于把b的类型和值都赋给了c
  c := b
  fmt.Println(c) //hello

  //c := a //No new variables on left side of :=


  //===================================
  //万能输出占位符: %v
  fmt.Printf("a=%v b=%v c=%v\n",a,b,c) //a= b=hello c=hello


  //===================================
  // ``反引号包裹的所有字符都是原样输出（单引号的话 是和java一样 只能接受一个字符）
  fmt.Println(`c:\nhello`) //c:\nhello
  fmt.Println(`
  afw
fwf


ff
`)
/*

     afw
   fwf


   ff
*/



  //=========================================
  //字符串常用操作

  //1. len()
  clen := len(c)
  fmt.Printf("lenth of c = %v\n",clen) //lenth of c = 5

  //2. 字符串拼接
  c=c+c
  fmt.Printf("c = %s\n",c)//c = hellohello
  //↕作用是一样的(fmt.Sprintf 和 +,都是对字符串进行拼接)
  c=fmt.Sprintf("%s%s",c,c)//c = hellohellohellohello
  fmt.Printf("c = %s\n",c)//c = hellohellohellohello

  //3. 分割 split
  ips := "10.108.34.30;10.108.34.31"
  ipArray := strings.Split(ips,";")
  fmt.Printf("first ip :%s\n", ipArray[0]) //first ip :10.108.34.30
  fmt.Printf("second ip :%s\n", ipArray[1]) //second ip :10.108.34.31

  //4. 包含Contains
  result := strings.Contains(ips,"10.108.34.30")
  fmt.Println(result) //true

  //5. HasPrefix、HasSuffix
  str := "http://baidu.com"
  if strings.HasPrefix(str,"http") {
    fmt.Printf("str is http url")
  }else {
    fmt.Printf("str is not http url")
  }

  if strings.HasSuffix(str,"baidu.com") {
    fmt.Printf("str is baidu url")
  }else {
    fmt.Printf("str is not baidu url")
  }

  //6 index,索引从0开始计数
  index := strings.Index(str,"baidu")
  fmt.Printf("baidu is index:%d\n", index) //str is http urlstr is baidu urlbaidu is index:7
  index = strings.LastIndex(str,"baidu")
  fmt.Printf("baidu last index:%d\n", index) //baidu last index:7

  //7. Join
  var strArr []string = []string{"10.237.8.2","10.237.8.3","10.237.8.3"}
  resultStr := strings.Join(strArr,";")
  fmt.Printf("resultStr=%s",resultStr) //resultStr=10.237.8.2;10.237.8.3;10.237.8.3
}
func main() {
  testStr()
}
